package com.event.web.controller.system;

import com.event.common.annotation.Log;
import com.event.common.core.controller.BaseController;
import com.event.common.core.domain.AjaxResult;
import com.event.common.core.page.TableDataInfo;
import com.event.common.enums.BusinessType;
import com.event.system.domain.SysEventOrder;
import com.event.system.service.ISysEventOrderService;
import com.event.system.service.ISysEventRegionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/system/eventOrder")
public class SysEventOrderController extends BaseController {

    private String prefix = "system/eventOrder";

    @Autowired
    private ISysEventOrderService sysEventOrderService;

    @Autowired
    private ISysEventRegionService sysEventRegionService;

    @RequiresPermissions("system:eventOrder:view")
    @GetMapping()
    public String eventOrder()
    {
        return prefix + "/eventOrder";
    }

    @RequiresPermissions("system:eventOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysEventOrder sysEventRoster)
    {
        startPage();
        List<SysEventOrder> list = sysEventOrderService.selectSysEventOrderList(sysEventRoster);
        return getDataTable(list);
    }

    /**
     * 预约通过
     */
    @RequiresPermissions("system:eventOrder:pass")
    @Log(title = "展会摊位申请通过", businessType = BusinessType.UPDATE)
    @PostMapping("/pass")
    @ResponseBody
    public AjaxResult pass(@Validated SysEventOrder sysEventOrder)
    {
        String userName = getSysUser().getUserName();
        sysEventOrder.setOrderRemarks(userName + "批准了申请");
        Long orderId = sysEventOrder.getOrderId();
        SysEventOrder sysEventOrder1 = sysEventOrderService.selectSysEventOrderByOrderId(orderId);
        Long regionId = sysEventOrder1.getRegionId();
        sysEventRegionService.updateByRegionIdToTwo(regionId);
        return toAjax(sysEventOrderService.updateByOrderIdToTwo(sysEventOrder));
    }

    /**
     * 预约拒绝
     */
    @RequiresPermissions("system:eventOrder:refuse")
    @Log(title = "展会摊位申请拒绝", businessType = BusinessType.UPDATE)
    @PostMapping("/refuse")
    @ResponseBody
    public AjaxResult refuse(@Validated SysEventOrder sysEventOrder)
    {
        String userName = getSysUser().getUserName();
        sysEventOrder.setOrderRemarks(userName + "拒绝了申请");
        Long orderId = sysEventOrder.getOrderId();
        SysEventOrder sysEventOrder1 = sysEventOrderService.selectSysEventOrderByOrderId(orderId);
        Long regionId = sysEventOrder1.getRegionId();
        sysEventRegionService.updateByRegionIdToThree(regionId);
        return toAjax(sysEventOrderService.updateByOrderIdToThree(sysEventOrder));
    }
}
